package com.gonetbar.sys.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.gonetbar.base.vo.ModelRecordStrUtil;
import com.gonetbar.sys.service.LoginService;
import com.gonetbar.sys.vo.UserInfo;
import com.gonetbar.sys.vo.UserInfoValidator;

/**
 * 
 * @Description:
 * 
 * @author weisd Email:xiyangdewuse@163.com
 * @date 2011-1-5 下午09:06:40
 * @version v1.0
 */
@Controller
@SessionAttributes(ModelRecordStrUtil.SESS_LOGIN_USER)
public class LoginAction {

	private LoginService loginService;

	@Required
	@Autowired
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	/**
	 * 
	 @ModelAttribute("user") public UserInfo createUser() {//for
	 *                         @SessionAttributes("LOGIN_USER")
	 *                         System.out.println("------创建-----------"); return
	 *                         new UserInfo(); }
	 * 
	 */

	/**
	 * 进入注册jsp
	 * 
	 * @return
	 */
	@RequestMapping("/regGoto.do")
	public ModelAndView queryToReg(@ModelAttribute("user") UserInfo user) {
		// model 验证码
		ModelAndView view = new ModelAndView();
		// resV.addObject("user", new UserInfo());
		view.setViewName("regIndex");
		return view;
	}

	/**
	 * 注册界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/regDo.do")
	public ModelAndView addRegUser(@ModelAttribute("user") UserInfo user,
			BindingResult result, SessionStatus status) {
		ModelAndView view = new ModelAndView();
		// validate
		new UserInfoValidator().validate(user, result);
		if (result.hasErrors()) {
			view.setViewName("regIndex");
		} else {
			loginService.addUser(user);			
			//resV.addObject(ModelRecordStrUtil.SESS_LOGIN_USER, user);// 手动session
			view.setViewName("redirect:/regSucc.do");// 成功后跳转
		}
		return view;
	}

	/**
	 * 跳转成功界面
	 * 进入这个界面的session中必须有值
	 * 
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/regSucc.do") 
//	public ModelAndView queryToSucc(@ModelAttribute(ModelRecordStrUtil.SESS_LOGIN_USER) UserInfo user,
	public ModelAndView queryToSucc(@ModelAttribute("user") UserInfo user,
			HttpServletRequest req, HttpServletResponse res) {
		ModelAndView view = new ModelAndView();
		UserInfo seUser = (UserInfo)req.getSession().getAttribute(ModelRecordStrUtil.SESS_LOGIN_USER);
		
		// session 中获取 user info
		System.out.println(user);
		view.setViewName("regSucc");
		return view;
	}

	/**
	 * 进入登陆jsp
	 * 
	 * @return
	 */
	@RequestMapping("/loginGoto.do")
	public ModelAndView queryToLogin(@ModelAttribute("user") UserInfo user) {
		// model 验证码
		ModelAndView view = new ModelAndView();
		view.setViewName("loginIndex");
		return view;
	}

	/**
	 * 登陆界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/loginDo.do")
	public ModelAndView queryLoginUser(@ModelAttribute("user") UserInfo user, BindingResult result, 
			SessionStatus status,HttpServletRequest request) {
		// public ModelAndView addRegUser(@ModelAttribute("LOGIN_USER") UserInfo
		// user,HttpServletRequest request,
		ModelAndView view = new ModelAndView();

		new UserInfoValidator().validate(user, result);

		System.out.println(loginService);
		System.out.println(user);

		if (result.hasErrors()) {
			view.setViewName("loginIndex");
		} else {
			UserInfo reUser = loginService.findUserByOne(user);
			if(null != reUser){
				view.setViewName("WEB-INF/myinfo/myIndex");
				view.addObject(ModelRecordStrUtil.SESS_LOGIN_USER, reUser);// 手动session
			}else{
				view.setViewName("loginIndex");
			}
		}
		return view;
	}

}
